package org.lmw.mm.ctrl;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.lmw.mm.dao.Coment;
import org.lmw.mm.dao.Coment_Post;
import org.lmw.mm.dao.Like_Post;
import org.lmw.mm.dao.Post;
import org.lmw.mm.dao.User;
import org.lmw.mm.utils.DateUtils;

public class PostCtrl extends BaseCtrl {
  /**
   * 
   * 此方法描述的是：帖子列表
   * 
   * @author: limengwei
   * @version: 2014-6-5 下午5:17:16
   */
  public void index() {
    // setAttr("posts", Post.dao.findAll(getParaToInt("userId")));
    setAttr("posts", Post.dao.findAll(getUID()));
    render("/comm/index.html");
  }

  /**
   * 
   * 此方法描述的是：发表帖子
   * 
   * @author: limengwei
   * @version: 2014-6-5 下午5:17:05
   */
  public void savePost() {
    Map<String, Object> m = new HashMap<String, Object>();
    m = gson.fromJson(getPara("objStr"), type_map);
    Post p = new Post().setAttrs(m).set("likeCount", 0).set("createTime", DateUtils.newDate2Str("yyyy-MM-dd HH:mm"));
    if (p.save()) {
      renderText(success_code);
    } else {
      renderText(error_code);
    }
  }

  /**
   * 
   * 此方法描述的是：点赞 / 取消赞
   * 
   * @author: limengwei
   * @version: 2014-6-5 下午5:16:54
   */
  public void likePost() {

    if (getParaToBoolean("like")) {
      renderText(Like_Post.dao.like(getParaToInt("postId"), getParaToInt("userId")));
    } else {
      renderText(Like_Post.dao.unLike(getParaToInt("postId"), getParaToInt("userId")));
    }
  }

  /**
   * 
   * 此方法描述的是：评论列表
   * 
   * @author: limengwei
   * @version: 2014-6-5 下午5:16:28
   */
  public void coment() {
    setAttr("post", Post.dao.findById(getPara(0)));
    setAttr("coments", Coment.dao.findAll(getParaToInt(0)));
    render("/comm/coment.html");
  }

  /**
   * 
   * 此方法描述的是：发表评论
   * 
   * @author: limengwei
   * @version: 2014-6-5 下午5:16:05
   */
  public void saveComent() {
    Map<String, Object> m = new HashMap<String, Object>();
    m = gson.fromJson(getPara("objStr"), type_map);
    Coment c = new Coment().setAttrs(m).set("createTime", DateUtils.newDate2Str("yyyy-MM-dd HH:mm"));

    if (Coment_Post.dao.isFirstComent(c.get("authorId"), c.get("pId"))) {
      // 第一次评论(生成随机头像，然后 保存评论内容)
      Coment_Post up = new Coment_Post().set("postId", c.get("pId")).set("userId", c.get("authorId")).set("head", RandomHeadUrl());
      if (up.save()) {
        if (c.save()) {
          renderText(success_code);
        } else {
          renderText(error_code);
        }
      } else {
        renderText(error_code);
      }
    } else {
      // 非第一次评论(直接保存评论内容)
      if (c.save()) {
        renderText(success_code);
      } else {
        renderText(error_code);
      }
    }
  }


  /**
   * 
   * 此方法描述的是：生成随机头像
   * 
   * @author: limengwei
   * @version: 2014-6-5 下午5:15:48
   */
  public String RandomHeadUrl() {
    int max = 450;
    int min = 1;
    Random random = new Random();
    int s = random.nextInt(max) % (max - min + 1) + min;
    System.out.println(s);
    String headUrl = "http://129.44.0.133:8081/res/img/h/h_" + s + ".png";
    return headUrl;
  }


  /** ------------------mobile-------------------- */


  public void m_postList() {
    res = new HashMap<String, Object>();
    res.put("res_code", success_code);
    res.put("objListStr", Post.dao.findAll(getParaToInt("userId")));
    renderJson(res);
  }

  public void m_savePost() {
    Map<String, Object> m = new HashMap<String, Object>();
    m = gson.fromJson(getPara("objStr"), type_map);
    Post p = new Post().setAttrs(m).set("likeCount", 0).set("createTime", DateUtils.newDate2Str("yyyy-MM-dd HH:mm"));
    res = new HashMap<String, Object>();
    if (p.save()) {
      res.put("res_code", success_code);
    } else {
      res.put("res_code", error_code);
    }
    renderJson(res);
  }

  public void m_comentList() {
    res = new HashMap<String, Object>();
    res.put("res_code", success_code);
    res.put("objListStr", Coment.dao.findAll(getParaToInt("postId")));
    renderJson(res);
  }


  public void m_saveComent() {
    res = new HashMap<String, Object>();
    Map<String, Object> m = new HashMap<String, Object>();
    m = gson.fromJson(getPara("objStr"), type_map);
    Coment c = new Coment().setAttrs(m).set("createTime", DateUtils.newDate2Str("yyyy-MM-dd HH:mm"));
    if (Coment_Post.dao.isFirstComent(c.get("authorId"), c.get("pId"))) {
      // 第一次评论(生成随机头像，然后 保存评论内容)
      Coment_Post up = new Coment_Post().set("postId", c.get("pId")).set("userId", c.get("authorId")).set("head", RandomHeadUrl());
      if (up.save()) {
        if (c.save()) {
          res.put("objStr", Coment.dao.findByComentId(c.getInt("id")));
          res.put("res_code", success_code);
        } else {
          res.put("res_code", error_code);
        }
      } else {
        res.put("res_code", error_code);
        
      }
    } else {
      // 非第一次评论(直接保存评论内容)
      if (c.save()) {
        res.put("objStr", Coment.dao.findByComentId(c.getInt("id")));
        res.put("res_code", success_code);
      } else {
        res.put("res_code", error_code);
      }
    }
    renderJson(res);
  }

  public void m_likePost() {
    res = new HashMap<String, Object>();
    if (getParaToBoolean("like")) {
      res.put("res_code", Like_Post.dao.like(getParaToInt("postId"), getParaToInt("userId")));
    } else {
      res.put("res_code",Like_Post.dao.unLike(getParaToInt("postId"), getParaToInt("userId")));
    }
    renderJson(res);
  }
  
}
